package com.hellord.ArmoryTools 
{
	import flash.display.Loader;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.events.*;
	import flash.net.URLRequestDefaults;
	import flash.net.URLRequestHeader;
	import flash.net.URLVariables;
	/**
	 * ...
	 * @author Matteo Cappelloni
	 */
	public class ArmoryLoader extends URLLoader
	{
		public static const EUROPE_ARMORY:String = "http://eu.wowarmory.com/";
		public static const USA_ARMORY:String = "http://www.wowarmory.com/";
		public static const KOREA_ARMORY:String = "http://kr.wowarmory.com/";
		
		private var _status:uint = 0;
		private var content:Object;
		private var lastReceived:XML;
		private var request:URLRequest;
		private var lastUrl:String;
		
		public var defaultArmory:String;
		
		public function ArmoryLoader() 
		{
			configureListeners(this);
			defaultArmory = EUROPE_ARMORY;
			lastUrl = defaultArmory;
		}
		public function getArena(ladder:String, battlegroup:String = null):Arena
		{
			return new Arena();
		}
		public function getCharacter(name:String, realm:String, region:String):Character
		{
			lastUrl = defaultArmory+"character-sheet.xml";
			request = new URLRequest(lastUrl);
			var variables:URLVariables = new URLVariables();
			variables.n = name;
			variables.r = realm;
			request.data = variables;
			
			if(connect(request))
			{
				_status = 1; // waiting for a response
			} else trace("No connection, retry later");
			
			return new Character();
		}
		public function getItem(id:uint):Item
		{
			lastUrl = defaultArmory+"item-tooltip.xml";
			request = new URLRequest(lastUrl);
			var variables:URLVariables = new URLVariables();
			variables.i = id;
			request.data = variables;
			
			//trace("Request: "+request.userAgent);
			if (connect(request))
			{
				_status = 1; // waiting for a response
			} else trace("No connection, search for others");
			
			return data;

		}
		public function search(string:String):void
		{
			var pattern:RegExp = /\d/;
			if (pattern.exec(string)) getItem(parseInt(string));
		}
		private function connect(armory:URLRequest):Boolean
		{
			try {
				
                load(armory);
				return true;
            } catch (error:Error) {
                trace("Unable to load requested document. " + error.message);
				return false;
            }
			
			return false;
		}

		private function configureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(Event.INIT, initHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            dispatcher.addEventListener(Event.OPEN, openHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            dispatcher.addEventListener(Event.UNLOAD, unLoadHandler);
        }

        private function completeHandler(event:Event):void {
			_status = 0; // back to offline
			
			var e:ArmoryToolsEvent = new ArmoryToolsEvent(ArmoryToolsEvent.ITEM_LOADED);
			var ml:XML = new XML(data);
			
			//trace(ml);
			e.itemName = ml.itemTooltips.itemTooltip.name;
			e.itemId = ml.itemTooltips.itemTooltip.id;
			
			var out:XML = new XML(ml.itemTooltips);
			out.setLocalName("item");
			
			var it:Item = new Item(out);
			e.itemObject = it;
			
			dispatchEvent(e);
        }
        private function httpStatusHandler(event:HTTPStatusEvent):void {
           // trace("httpStatusHandler: " + event);
		   
        }

        private function initHandler(event:Event):void {
            //trace("initHandler: " + event);
        }

        private function ioErrorHandler(event:IOErrorEvent):void {
            //trace("ioErrorHandler: " + event);
			_status = 0; // back to offline
        }

        private function openHandler(event:Event):void {
            //trace("openHandler: " + event);
        }

        private function progressHandler(event:ProgressEvent):void {
            //trace("progressHandler: bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
        }

        private function unLoadHandler(event:Event):void {
            //trace("unLoadHandler: " + event);
        }

	}

}